
// buf的最小尺寸.
const MIN_LOG_BUF_SIZE: usize = 4 * 1024 * 1024; // 4M.
                                                 // 最多允许有多少个buf未flush.
const MIN_MAX_PENDING_BUFS: usize = 4;
// 每隔3s自动对log进行一次flush.
const MIN_FLUSH_INTERVAL_MS: usize = 1000; // 1s


pub struct LogOption {
    pub level: Level,
    pub log_type: LogType,
    pub console: bool,
    pub log_path: Option<String>,
    pub rolling: Rolling,
    pub log_buf_size: usize,
    pub max_pending_bufs: usize,
    pub flush_interval_ms: usize,
}

impl Default for LogOption {
    fn default() -> Self {
        LogOption {
            level: Level::Warn,
            log_type: LogType::ErrorLog,
            console: true,
            log_path: None,
            rolling: Rolling::Hour,
            log_buf_size: MIN_LOG_BUF_SIZE,
            max_pending_bufs: MIN_MAX_PENDING_BUFS,
            flush_interval_ms: MIN_FLUSH_INTERVAL_MS,
        }
    }
}

#[allow(dead_code)]
impl LogOption {
    pub fn set_level(mut self, level: Level) -> Self {
        self.level = level;
        self
    }

    pub fn set_log_type(mut self, log_type: LogType) -> Self {
        self.log_type = log_type;
        self
    }

    pub fn set_console(mut self, enable: bool) -> Self {
        self.console = enable;
        self
    }

    pub fn get_console(&self) -> bool {
        self.console
    }

    pub fn set_log_path(mut self, path: String) -> Self {
        self.log_path = Some(path);
        self
    }

    pub fn set_rolling(mut self, rolling: Rolling) -> Self {
        self.rolling = rolling;
        self
    }

    pub fn set_log_buf_size(mut self, buf_size: usize) -> Self {
        let buf_size = buf_size.min(MIN_LOG_BUF_SIZE);
        self.log_buf_size = buf_size;
        self
    }

    pub fn set_max_pending_bufs(mut self, pending_bufs: usize) -> Self {
        let pending_bufs = pending_bufs.min(MIN_MAX_PENDING_BUFS);
        self.max_pending_bufs = pending_bufs;
        self
    }

    pub fn set_flush_interval_ms(mut self, interval_ms: usize) -> Self {
        let interval_ms = interval_ms.min(MIN_FLUSH_INTERVAL_MS);
        self.flush_interval_ms = interval_ms;
        self
    }
}
